package com.yanfan.maintain.controller;

import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.utils.SecurityUtils;
import com.yanfan.energy.domain.ProductionStaff;
import com.yanfan.energy.domain.ProductionStaffHead;
import com.yanfan.energy.domain.ProductionStaffRequest;
import com.yanfan.energy.service.ExcelService;
import com.yanfan.energy.service.ProductionStaffService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * (ProductionStaff)表控制层
 *
 * @author makejava
 * @since 2023-01-30 14:24:25
 */
@RestController
@RequestMapping("/production/productionStaff")
public class ProductionStaffController {
    /**
     * 服务对象
     */
    @Resource
    private ProductionStaffService productionStaffService;

    @Autowired
    private ExcelService excelService;

    /**
     * 分页查询
     *
     * @param productionStaffRequest 筛选条件
     * @return 查询结果
     */
    @PostMapping("/queryByPage")
    public AjaxResult queryByPage(@RequestBody ProductionStaffRequest productionStaffRequest) {
        PageHelper.startPage(productionStaffRequest.getCurrPage(), productionStaffRequest.getPageSize());
        PageInfo<ProductionStaff> pageInfo = new PageInfo<>(productionStaffService.queryByPage(productionStaffRequest));
        return AjaxResult.success("查询成功", pageInfo);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<ProductionStaff> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.productionStaffService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param productionStaff 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<ProductionStaff> add(ProductionStaff productionStaff) {
        return ResponseEntity.ok(this.productionStaffService.insert(productionStaff));
    }

    /**
     * 编辑数据
     *
     * @param productionStaff 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<ProductionStaff> edit(ProductionStaff productionStaff) {
        return ResponseEntity.ok(this.productionStaffService.update(productionStaff));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Long id) {
        return ResponseEntity.ok(this.productionStaffService.deleteById(id));
    }

    /**
     * 下载
     *
     * @return 查询结果
     */
    @PostMapping("/queryListReport")
    public void queryListReport(@RequestParam(required = false) String number, @RequestParam(required = false) String name, @RequestParam(required = false) String job, @RequestParam(required = false) Long phoneNumber, HttpServletResponse response) throws IOException {
        ProductionStaffRequest productionStaffRequest = new ProductionStaffRequest();
        productionStaffRequest.setNumber(number);
        productionStaffRequest.setName(name);
        productionStaffRequest.setJob(job);
        productionStaffRequest.setPhoneNumber(phoneNumber);
        List<ProductionStaff> productionStaffs = productionStaffService.queryByPage(productionStaffRequest);
        excelService.downloadList(ProductionStaffHead.class, productionStaffs, response, "人员统计列表");
    }

    /**
     * 详情页 右边的
     *
     * @param productionStaffRequest 筛选条件
     * @return 查询结果
     */
    @PostMapping("/detailInfo")
    public AjaxResult detailInfo(@RequestBody ProductionStaffRequest productionStaffRequest) {
        Long userId = null;
        if (productionStaffRequest == null || productionStaffRequest.getUserId() == null) {
            userId = SecurityUtils.getLoginUser().getUser().getUserId();
        } else {
            userId = productionStaffRequest.getUserId();
        }
        Map<String, Object> resp = productionStaffService.detailInfo(userId);
        return AjaxResult.success("查询成功", resp);
    }

}

